
package com.afd.ozblip.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.ReviewListAdapter;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.entity.ReviewEntity;
import com.afd.ozblip.entity.VipEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UserVipListActivity extends BaseActivity {

    /****************** 视图变量 **************************/
    private ViewPager viewPager;
    private ArrayList<View> pageViews;
    private ImageView imageView;
    private ImageView[] imageViews;
    // 包裹小圆点的LinearLayout
    private ViewGroup group;
    private LinearLayout tipLayout;
    private LinearLayout commonRight;
    private TextView tipTv;
    /****************** 其它变量 **************************/
    private String nowVipId;
    private List<VipEntity> vipList;

    // 当前索引

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_vip_list);
        // 启动返回功能
        toBack();
        // 寻找视图
        findViews();
        // 设置监听
        setListeners();
        // 获取参数
        getBundle();
        // 获取数据
        requestUserVipListData();
    }

    /**
     * 寻找视图
     */
    public void findViews() {
        this.tipLayout = (LinearLayout) findViewById(R.id.ll_user_vip_tip);
        this.commonRight = (LinearLayout) findViewById(R.id.ll_common_right);
        this.tipTv = (TextView) findViewById(R.id.tip_text_noData);
    }

    /**
     * 设置监听
     */
    public void setListeners() {
        View.OnClickListener moreClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_common_right:
                        Dialog alertDialog = new AlertDialog.Builder(
                                UserVipListActivity.this)
                                .setTitle("提示")
                                .setMessage("确定删除当前会员卡吗？")
                                .setIcon(android.R.drawable.ic_menu_help)
                                .setPositiveButton("确定",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                int nowIndex = viewPager
                                                        .getCurrentItem();
                                                nowVipId = vipList.get(nowIndex)
                                                        .getVipId();
                                                requestDeleteUserVipData();
                                            }
                                        })
                                .setNegativeButton("取消",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                dialog.dismiss();
                                            }
                                        }).create();
                        alertDialog.show();
                        break;

                    default:
                        break;
                }
            }
        };
        this.commonRight.setOnClickListener(moreClick);
    }

    /**
     * 获取评论列表数据
     */
    private void requestUserVipListData() {
        if (commonUserInfo == null) {
            return;
        }
        AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
                        nearbyBusinessParamMap.put("userId",
                                commonUserInfo.getUserId());
                        nearbyBusinessParamMap.put("pageCnt", "1");
                        nearbyBusinessParamMap.put("pageRow", "40");
                        nearbyBusinessParamMap.put("language", commonLanguage);
                        // 发送请求
                        byte[] bReviewList = HttpUtil.requestByPost(
                                UserVipListActivity.this,
                                Constants.USER_VIP_LIST_API,
                                nearbyBusinessParamMap);
                        if (bReviewList != null) {
                            vipList = new ParseJson()
                                    .parseUserVipListJson(bReviewList);
                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                        if (vipList != null && vipList.size() > 0) {
                            tipTv.setVisibility(8);
                            tipLayout.setVisibility(0);
                            commonRight.setVisibility(0);
                            addPageView();
                        } else {
                            tipTv.setVisibility(0);
                            tipTv.setText(R.string.noData_text);
                            tipLayout.setVisibility(8);
                            commonRight.setVisibility(8);
                        }
                    }

                });
        ald.execute(0);
    }

    /**
     * 请求删除当前VIP
     */
    private void requestDeleteUserVipData() {
        if (commonUserInfo == null) {
            return;
        }
        AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
                        nearbyBusinessParamMap.put("userId",
                                commonUserInfo.getUserId());
                        nearbyBusinessParamMap.put("memberCardId", nowVipId);
                        // 发送请求
                        byte[] bReviewList = HttpUtil.requestByPost(
                                UserVipListActivity.this,
                                Constants.USER_DELETE_VIP_API,
                                nearbyBusinessParamMap);
                        if (bReviewList != null) {

                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                        requestUserVipListData();
                    }

                });
        ald.execute(0);
    }

    private void addPageView() {
        LayoutInflater inflater = getLayoutInflater();
        pageViews = new ArrayList<View>();
        for (int i = 0, len = vipList.size(); i < len; i++) {
            final VipEntity vip = vipList.get(i);
            View v = inflater.inflate(R.layout.user_vip_list_item, null);

            TextView businessNameTv = (TextView) v
                    .findViewById(R.id.tv_vip_list_item_businessName);
            TextView vipNameTv = (TextView) v
                    .findViewById(R.id.tv_vip_list_item_vipName);
            TextView vipNoTv = (TextView) v
                    .findViewById(R.id.tv_vip_list_item_vipNo);
            TextView vipTitleTv = (TextView) v
                    .findViewById(R.id.tv_vip_list_item_title);
            TextView vipContentTv = (TextView) v
                    .findViewById(R.id.tv_vip_list_item_content);
            LinearLayout vipLayout = (LinearLayout) v
                    .findViewById(R.id.ll_vip_list_item_vipLayout);
            businessNameTv.setText(vip.getBusinessName());
            vipNameTv.setText(vip.getVipUserName());
            vipNoTv.setText(vip.getVipId());
            vipTitleTv.setText(vip.getVipTitle());
            vipContentTv.setText(vip.getVipContent());
            vipLayout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent businessIntent = new Intent(
                            UserVipListActivity.this,
                            BusinessInfoActivity.class);
                    businessIntent.putExtra("businessId", vip.getBusinessId());
                    startActivity(businessIntent);
                }
            });
            pageViews.add(v);
        }
        imageViews = new ImageView[pageViews.size()];
        initViewPager();
    }

    private void initViewPager() {
        group = (ViewGroup) findViewById(R.id.ll_vip_dot);
        viewPager = (ViewPager) findViewById(R.id.vp_vip_content);

        for (int i = 0, len = pageViews.size(); i < len; i++) {
            imageView = new ImageView(UserVipListActivity.this);
            imageView.setLayoutParams(new LayoutParams(15, 15));
            imageView.setPadding(20, 0, 20, 0);
            imageViews[i] = imageView;

            if (i == 0) {
                // 默认选中第一张图片
                imageViews[i]
                        .setBackgroundResource(R.drawable.page_indicator_focused);
            } else {
                imageViews[i].setBackgroundResource(R.drawable.page_indicator);
            }

            group.addView(imageViews[i]);
        }
        viewPager.setAdapter(new GuidePageAdapter());
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());
    }

    /**
     * 获取传递的参数
     */
    private void getBundle() {
        // Intent intent = getIntent();
    }

    // 指引页面数据适配器
    private class GuidePageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(pageViews.get(arg1));
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(pageViews.get(arg1));
            return pageViews.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {

        }

        @Override
        public void finishUpdate(View arg0) {

        }
    }

    // 指引页面更改事件监听器
    private class GuidePageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0, len = imageViews.length; i < len; i++) {
                imageViews[arg0]
                        .setBackgroundResource(R.drawable.page_indicator_focused);

                if (arg0 != i) {
                    imageViews[i]
                            .setBackgroundResource(R.drawable.page_indicator);
                }
            }
        }
    }
}
